home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15716 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  88 lines

  1. Newsgroups: comp.lang.c
  2. Path: netcom.com!smryan
  3. From: smryan@netcom.com (@#$%!?!)
  4. Subject: Re: Reusable and Generic Functions
  5. Message-ID: <smryanDq6L1B.HIG@netcom.com>
  6. Organization: The Programmer formerly known as S M Ryan
  7. X-Newsreader: TIN [version 1.2 PL1]
  8. References: <4la9k2$76o@wormer.fn.net>
  9. Date: Sat, 20 Apr 1996 21:58:22 GMT
  10. Sender: smryan@netcom4.netcom.com
  11.  
  12. : Is there a simple way to handle operations on both of those lists
  13. : using the same set of functions?  Say for adding links we have this
  14. : function:
  15.  
  16. Yes. You too can learn to abuse cpp in your own home.
  17.  
  18. One way is to write a single define that can kill some compilers:
  19.  
  20.     #define    generic(typename,datatype) \
  21.         typedef struct typename { \
  22.             datatype data; \
  23.             struct typename *next; \
  24.         } typename;\
  25.         \
  26.         typename *typename##_add(typename *next,datatype data) {\
  27.             typename *temp = malloc(sizeof(typename)); \
  28.             if (!temp) {...} \
  29.             temp->data = data; \
  30.             temp->next = next; \
  31.             return next; \
  32.         }\
  33.         \
  34.         void typename##_delete(typename *list) {\
  35.             while (list) { \
  36.                 typename *next=list->next; \
  37.                 free(list); list = next; \
  38.             } \
  39.         }
  40.  
  41. And then
  42.     generic(FOO,int)
  43. will define FOO, FOO_add, and FOO_delete.
  44.     generic(BAR,float)
  45. will define BAR, BAR_add, and BAR_delete.
  46.  
  47.  
  48. You can also put the code in another file so that generic.type is
  49.  
  50.     typedef struct typename{
  51.         datatype data;
  52.         struct typename *next;
  53.     } typename;
  54.     
  55.     typename *addFunction(typename *next,datatype data) {
  56.         ...
  57.     }
  58.     #ifdef deleteFunction
  59.         typename *deleteFunction(typename *list) {
  60.             ...
  61.         }
  62.     #endif
  63.  
  64. and then instantiate with
  65.  
  66.     #define typename    FOO
  67.     #define datatype    int
  68.     #define addFunction    sllist_add
  69.     #include "generic.type"
  70.     #undef  typename
  71.     #undef  datatype
  72.     #undef  addFunction
  73.  
  74.     #define typename    BAR
  75.     #define datatype    float
  76.     #define addFunction    barr_wed
  77.     #define deleteFunction    arnold_divorce
  78.     #include "generic.type"
  79.     #undef  typename
  80.     #undef  datatype
  81.     #undef  addFunction
  82.     #undef  deleteFunction
  83. -- 
  84. The Queen who loves, the Queen of life,    | smryan@netcom.com  PO Box 1563
  85. the Queen who straits, the Queen of strife;|          Cupertino, California
  86. with gasp of death or gift of breath       | (xxx)xxx-xxxx            95015
  87. she brings the choice of birth or knife.   |         I don't use no smileys
  88.